﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Daidalos.Shaders
{

	/// <summary>
	///		Implements an abstract base class, to derive shader sets
	///		from. A shader set represents a shader effect composed of
	///		a vertex- and a fragmentshader programm, as well as
	///		their specific configuration.
	/// </summary>
	public abstract class ShaderSet
	{

		#region Attributes
		/// <summary>
		///		The name of the shader set.
		/// </summary>
		protected string name;

		/// <summary>
		///		The underlying Direct3D shader object.
		/// </summary>
		protected Microsoft.DirectX.Direct3D.Effect shaderSet = null;
		#endregion

		#region Properties
		/// <summary>
		///		Implements a selector for the shader set's name.
		/// </summary>
		public string Name
		{
			get
			{
				return name;
			}
		}
		#endregion

		#region Constructors
		/// <summary>
		///		Constructrs a shader set from a .fx file.
		/// </summary>
		/// <param name="name">
		///		The name of the shader.
		/// </param>
		public ShaderSet(string name)
		{
			// Begin to load the shader.
			Utilities.LogManager.Instance.Log("Loading shader set \"" + name + "\".");

			// Store the name.
			this.name = name;

			// Try to load the shader.
			string compilerErrors = null;
			try
			{
				shaderSet = Effect.FromFile(Graphics.RenderDevice.Instance.Device, name, null, null, ShaderFlags.None,
								null, out compilerErrors);
			}
			catch (InvalidCallException)
			{
				Utilities.LogManager.Instance.Log("  + Failed due to invalid loader-invocation, probably invalid parameters!");
				throw new Exception("Loading the shader \"" + name + "\" failed due to an invalid invocation!");
			}
			catch (InvalidDataException)
			{
				Utilities.LogManager.Instance.Log("  + Failed due to an invalid shader file!");
				throw new Exception("Loading the shader \"" + name + "\" failed due to an invalid shader file!");
			}
			catch (OutOfVideoMemoryException)
			{
				Utilities.LogManager.Instance.Log("  + Failed due to insufficient video memory!");
				throw new Exception("Loading the shader \"" + name + "\" failed due to insufficient video memory.");
			}

			// End loading the shader.
			Utilities.LogManager.Instance.Log("  + Loaded the shader file.");

			// Register with the resource manager.
			Utilities.ResourceManager.Instance.RegisterShader(GetInternalName(), this);
			Utilities.LogManager.Instance.Log("  + Registered the shader.");
		}

		/// <summary>
		///		Releases AGP memory used by the shader.
		/// </summary>
		~ShaderSet()
		{
			// Check if the shader is allocated and has not yet been released.
			if (shaderSet != null && !shaderSet.Disposed)
			{
				// Dipose of the shader.
				shaderSet.Dispose();
				shaderSet = null;
			}
		}
		#endregion

		#region Methods
		/// <summary>
		///		This method loads the shader set and must be called 
		///		before rendering geometry with this shader.
		/// </summary>
		public abstract void Begin();

		/// <summary>
		///		This method's invocation resembles the end of
		///		the local use of that shader set.
		/// </summary>
		public abstract void End();

		/// <summary>
		///		This method is used to link the effect handles
		///		to the values in the shader programm.
		/// </summary>
		protected virtual void LinkParameters()
		{
			// Nothing to do here!
		}

		/// <summary>
		///		This method is used to determine the internal
		///		name of the shader.
		/// </summary>
		/// <returns>
		///		Returns the internal name of that shader.
		/// </returns>
		protected abstract string GetInternalName();
		#endregion

	}

}
